package ar.com.coonocer.joy.mdg.templatehelpers;

import java.util.ArrayList;
import java.util.List;

import ar.com.coonocer.joy.mdg.xmi.core.Class;
import ar.com.coonocer.joy.mdg.xmi.core.ModelElement;
import ar.com.coonocer.joy.mdg.xmi.core.Stereotype;
import ar.com.coonocer.joy.mdg.xmi.core.TaggedValue;
import ar.com.coonocer.joy.mdg.xmi.model.Package;

public class StereotypeUtil {

	static public String getTaggedValueValue(ModelElement modelElement, String tagName) {
		//
		List<TaggedValue> list = modelElement.getTaggedValue();
		for (TaggedValue taggedValue : list) {
			if (taggedValue.getType() != null && taggedValue.getType().getName() != null && taggedValue.getType().getName().equals(tagName)) {
				if (!taggedValue.getDataValue().isEmpty()) {
					return taggedValue.getDataValue().get(0);
				}
			}
		}
		//
		return null;
	}

	static public boolean hasStereotype(ModelElement modelElement, String stereotypeName) {
		//
		List<Stereotype> list = modelElement.getStereotype();
		for (Stereotype s : list) {
			if (s.getName() != null && s.getName().equals(stereotypeName)) {
				return true;
			}
		}
		//
		return false;
	}

	//todo generalizar para cualquier tipo
	public static List<Class> getNestedClassesByStereotype(Package package_, String stereotypeName) {
		List<Class> list = new ArrayList<Class>();
		//
		List<ModelElement> ps = package_.getOwnedElement();
		for (ModelElement me : ps) {
			if (me instanceof Package){
				Package p = (Package) me;
				List<Class> aux = getNestedClassesByStereotype(p, stereotypeName);
				//
				if (aux != null) {
					list.addAll(aux);
				}
			} else if (me instanceof Class) {
				Class cl = (Class) me;
				if (cl.getIsRoot()==false){
					if (StereotypeUtil.hasStereotype(cl, stereotypeName)){
						list.add((Class) me);
					}
				}
			}
		}
		//
		return list;
	}

}
